(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.site.createVerifyController', createVerifyController);
	createVerifyController.$inject = ['$q', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'siteService' ];
	function createVerifyController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, siteService) {
		var ctrl = this, siteInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.authService = authService;
		ctrl.site = {};
		siteService.initPageCallback(ctrl);
		siteService.initTableConfig($scope, ctrl, layoutConfig);
		ctrl.contactInnerGridOptions.enableRowHeaderSelection = false;
		ctrl.serviceGridOptions.enableRowHeaderSelection = false;
		ctrl.subcompanyGridOptions.enableRowHeaderSelection = false;
		ctrl.notifyGridOptions.enableRowHeaderSelection = false;
		ctrl.eventGridOptions.enableRowHeaderSelection = false;
		ctrl.selectedPropertyInnerGridOptions.enableRowHeaderSelection = false;
		ctrl.reportInnerGridOptions.enableRowHeaderSelection = false;
		ctrl.addressInnerGridOptions.enableRowHeaderSelection = false;
		ctrl.$onInit = function() {
			var site, layout, siteOptions, localeCodeOptions, promise, promises = [];
			promise = authService.createVerify(siteInfo);
			promises.push(promise);
			
			promise = siteService.getLayoutConfig('createVerify');
			promises.push(promise);
			
			promise = siteService.searchEvents(ctrl, ctrl.eventSearchCriteria);
			promises.push(promise);
			
			promise = siteService.loadSiteOptions();
			promises.push(promise);
			
			promise = siteService.getLocaleOptions();
			promises.push(promise);
			
			$q.all(promises).then(function(response) {
				site = response[0].data.model;
				site.taskId = response[0].data.task.taskId;
				site.localeCodes = site.localeCodes || [];
				layout = response[1].data ? response[1].data : response[1];
				siteOptions = response[3];
				localeCodeOptions = response[4];
				if (layout && site) {
					siteService.initSiteOptionData(ctrl, siteOptions);
					siteService.initReport($scope, ctrl);
					siteService.initEdiProfile(site);
					siteService.resetDynaProperties(ctrl, layout, site);
					siteService.initSiteUserTheme(site, ctrl);
					ctrl.layoutConfig = layout;
					ctrl.site = site;
					siteService.setLocaleOptions(ctrl);
					ctrl.site.siteReports = ctrl.site.siteReports || [];
					angular.forEach(ctrl.site.siteReports, function(siteReport, key) {
						if (siteReport && siteReport.dataStatus != "V") {
							ctrl.siteReport.serviceCode = siteReport.report.serviceCode;
						}
						if (!ctrl.site['siteReports' + siteReport.report.serviceCode]) {								
							ctrl.site['siteReports' + siteReport.report.serviceCode] = [];
						}
						ctrl.site['siteReports' + siteReport.report.serviceCode].push(siteReport);
					});
				}
			});
		};
		
		ctrl.lookuplocaleCodes = function(){
			var passparams=[];
			if(ctrl.localeOptions && ctrl.localeOptions.data.length>0){
				siteService.getLocaleCodesTwo(ctrl).then(function(){
					for(var i=0; i<ctrl.localeOptions.data.length; i++){
						for(var j=0; j<ctrl.localeCodesData.data.length; j++){
							if(ctrl.localeOptions.data[i].value==ctrl.localeCodesData.data[j].value){
							    var a={};
							    a.localeCode=ctrl.localeCodesData.data[j].value;
								passparams.push(a);
							}
						}
					}
					
				});
			}
		    popup(passparams);
		};
		
		function popup(parameter){
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.localeCodes.popupView',
				'title' : 'title.dialogs.info',
				'params': parameter,
				'btnCallback' : {
					'ok' : function(localeCodes) {
						if (!localeCodes || localeCodes.length == 0) {
							modalService.openErrorDialogs({
								messages : [ 'title.dialogs.info.norecsel' ]
							});
						}
						ctrl.site.localeCodes=[];
						if(localeCodes!=null && localeCodes.length>0){
							for(var i=0; i<localeCodes.length; i++){
							    ctrl.site.localeCodes.push(localeCodes[i].localeCode);
							}
						}
						siteService.setLocaleOptions(ctrl);
						
				     }
				}
			});
		};
		
		ctrl.loadDynaparms = function (){
			var params = {};
			// for get dynaparm page
			params.glPostMgrDynaparm = {};
			if(ctrl.site.glPostMgrDynaparm){
				params.glPostMgrDynaparm.dynamicParams = ctrl.site.glPostMgrDynaparm.dynamicParams;
			}
			if( ctrl.site.glPostMgr){
				params.glPostMgr = ctrl.site.glPostMgr;
			}
			modalService.openCustomDialogs({
				   'title': 'GL Posting Dynaparms',
				   'dialogsModule' : 'cda.glPostMgr.popupview',
				   'params': params,
				   'btnCallback' : {
						'ok' : function(resultList) {
							ctrl.site.glPostMgrDynaparm = {};
							ctrl.site.glPostMgrDynaparm = resultList;
						}
					}	
				});
		};
		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.site.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.site).then(approveSuccessful, submitFailed);
					}
				}
			});
		};

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.site.comments = comments;
						authService.createVerifyReject(ctrl.site).then(rejectSuccessful, submitFailed);
					}
				}
			});			
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.site.list',
					'state' : 'cda.site.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.site.list',
					'state' : 'cda.site.list',
					'params' : {}
				}]
			});
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
